package cn.fl.riskctrl.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;

import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.comm.facade.upload.condition.SysUlFilesRecQueryCondition;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.cache.framework.utils.PropertiesCacheUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.framework.constant.DictionaryConstant;
import cn.fl.ref.fncprjbscinfo.util.DateUtil;
import cn.fl.riskctrl.service.IRcCstSocialRlService;
import cn.fl.riskctrl.transform.RcCstSocialRlVOTransform;
import cn.fl.riskctrl.vo.RcCstSocialRlVO;
import cn.fl.system.service.ISysUlFilesRecService;
import cn.fl.system.vo.SysUlFilesRecVO;
import fl.constant.facade.framework.constant.UploadConstant;
import fl.riskctrl.facade.riskctrl.api.IRiskCtrlSearchApiService;
import fl.riskctrl.facade.riskctrl.condition.RcCstSocialRlQC;
import fl.riskctrl.facade.riskctrl.dto.RcCstSocialRlDTO;
@Service
public class RcCstSocialRlServiceImpl implements IRcCstSocialRlService{
	private static final String RL_TYP_CD_5 = "5";//配偶
	private static final String RL_TYP_CD_8 = "8";//担保人
	/**
	 * 日志
	 */
	private static final Logger logger = LoggerFactory.getLogger(IRcCstSocialRlService.class);
	
	/**
	 * RcCstSocialRl 服务层BEAN
	 */
	@Autowired
	private IRiskCtrlSearchApiService riskCtrlSearchApiService;
	
	@Autowired
	private ISysDictionaryApiService sysDictionaryApiService;
	
	/**
	 * SysUlFilesRec 服务层BEAN
	 */
	@Autowired
	private ISysUlFilesRecService sysUlFilesRecService;
	@Override
	public List<RcCstSocialRlVO> searchRcCstSocialRlList(RcCstSocialRlQC qc) {
		if(qc == null) qc = new RcCstSocialRlQC();
		List<RcCstSocialRlVO> voList = new ArrayList<>();
		List<RcCstSocialRlDTO> list = riskCtrlSearchApiService.searchRcCstSocialRlList(qc);
		if(CollectionUtils.isNotEmpty(list)){
			Map<String, Map<String,String>> cdMap = null;
			List<String> codeGrpCdList = new ArrayList<>();
			//证件类型
			codeGrpCdList.add(DictionaryConstant.CODE_GRP_CERT_TYP_CD);
			//亲属关系
			codeGrpCdList.add(DictionaryConstant.CODE_GRP_RL_TYP_CD);
			//性别
			codeGrpCdList.add(DictionaryConstant.CODE_GRP_GENDER_CD);
			//数据字典
			try{
				cdMap = sysDictionaryApiService.searchMultiGroupToMap(CurrentThreadContext.getFlServiceSysCd(), 
						CurrentThreadContext.getCurrentOrgCd(), codeGrpCdList);
			}catch(Exception e){
				BaseLogUtils.info(logger, "searchRcCstSocialRlList", "获取数据字典项失败!" + JSON.toJSONString(qc));
			}
			if(CollectionUtils.isMapEmpty(cdMap)){
				cdMap = new HashMap<>();
			}
			Map<String, String> certTypCdMap = cdMap.get(DictionaryConstant.CODE_GRP_CERT_TYP_CD);
			Map<String, String> rlTypCdMap = cdMap.get(DictionaryConstant.CODE_GRP_RL_TYP_CD);
			Map<String, String> genderCdMap = cdMap.get(DictionaryConstant.CODE_GRP_GENDER_CD);
			voList = RcCstSocialRlVOTransform.toVOList(list);
			List<String> pkList = new ArrayList<>();
			for (RcCstSocialRlVO rcCstSocialRlVO : voList) {
				if(CollectionUtils.isMapNotEmpty(certTypCdMap)){
					rcCstSocialRlVO.setCertTypCdNm(certTypCdMap.get(rcCstSocialRlVO.getCertTypCd()));
				}
				if(CollectionUtils.isMapNotEmpty(rlTypCdMap)){
					rcCstSocialRlVO.setRlTypCdNm(rlTypCdMap.get(rcCstSocialRlVO.getRlTypCd()));
				}
				if(CollectionUtils.isMapNotEmpty(genderCdMap)){
					rcCstSocialRlVO.setGenderCdNm(genderCdMap.get(rcCstSocialRlVO.getGenderCd()));
				}
				pkList.add(String.valueOf(rcCstSocialRlVO.getId()));
				Date birthday = rcCstSocialRlVO.getBirthday();
				if(birthday != null){
					rcCstSocialRlVO.setBirthdayStr(DateUtil.getStringDateByDate(birthday,"-"));
				}
			}
			//获取授权书图片路径
			getAuthFilePath(pkList,voList);
		}
		return voList;
	}
	/**
	 * 授权书信息
	 * @param pkList
	 * @param voList
	 */
	private void getAuthFilePath(List<String> pkList, List<RcCstSocialRlVO> voList) {
		if(CollectionUtils.isEmpty(voList) || CollectionUtils.isEmpty(pkList)){
			return;
		}
		SysUlFilesRecQueryCondition condition = new SysUlFilesRecQueryCondition();
		condition.setPkIdList(pkList);
		condition.setSysCd(CurrentThreadContext.getCurrentSysCd());//系统代码-必须条件
		List<SysUlFilesRecVO> sysUlFilesRecList = this.sysUlFilesRecService.searchList(condition);
		String cfgcdOne = PropertiesCacheUtils.getProperty(UploadConstant.CFG_RCT_CST_BSC_CD);
		String cfgCstIdCd = PropertiesCacheUtils.getProperty(UploadConstant.CFG_CST_ID_CD);
		String cfgDtlCstIdCd = PropertiesCacheUtils.getProperty(UploadConstant.CFG_DTL_CST_ID_CD);
		String cfgCstMateIdCd = PropertiesCacheUtils.getProperty(UploadConstant.CFG_CST_MATE_ID_CD);
		String cfgDtlCstMateIdCd = PropertiesCacheUtils.getProperty(UploadConstant.CFG_DTL_CST_MATE_ID_CD);
		Map<String, SysUlFilesRecVO> mapRctOne = new HashMap<>();
		Map<String, SysUlFilesRecVO> cfgCstIdCdMap = new HashMap<>();
		Map<String, SysUlFilesRecVO> cfgCstIdCdMateMap = new HashMap<>();
		for (SysUlFilesRecVO sysUlFilesRecVO: sysUlFilesRecList) {
			String pkId = sysUlFilesRecVO.getPkId();
			String cfgcd = sysUlFilesRecVO.getCfgCd();
			String cfgDtlCd = sysUlFilesRecVO.getCfgDtlCd();
			if (cfgcdOne != null && cfgcdOne.equals(cfgcd)) {
				mapRctOne.put(pkId, sysUlFilesRecVO);
			}
			//身份证图片-担保人
			if (cfgCstIdCd != null && cfgCstIdCd.equals(cfgcd) && cfgDtlCd.equals(cfgDtlCstIdCd)) {
				cfgCstIdCdMap.put(pkId, sysUlFilesRecVO);
			}
			
			//身份证图片-配偶
			if (cfgCstIdCd != null && cfgCstMateIdCd.equals(cfgcd) && cfgDtlCd.equals(cfgDtlCstMateIdCd)) {
				cfgCstIdCdMateMap.put(pkId, sysUlFilesRecVO);
			}
			
		}
		for (RcCstSocialRlVO rcCstSocialRlVO : voList) {
			String cstId = String.valueOf(rcCstSocialRlVO.getId());
			if (!mapRctOne.isEmpty() && mapRctOne.get(cstId) != null) {
				rcCstSocialRlVO.setAuthFileNm(mapRctOne.get(cstId).getFileNm());
				rcCstSocialRlVO.setAuthFilePath(mapRctOne.get(cstId).getFilePath());
			} else {
				rcCstSocialRlVO.setAuthFileNm("");
				rcCstSocialRlVO.setAuthFilePath("");
			}
			//担保人
			if(rcCstSocialRlVO.getRlTypCd().equals(RL_TYP_CD_8) && !cfgCstIdCdMap.isEmpty() && cfgCstIdCdMap.get(cstId) != null){
				rcCstSocialRlVO.setIdCardFileNm(cfgCstIdCdMap.get(cstId).getFileNm());
				rcCstSocialRlVO.setIdCardFilePath(cfgCstIdCdMap.get(cstId).getFilePath());
			}else{
				rcCstSocialRlVO.setIdCardFileNm("");
				rcCstSocialRlVO.setIdCardFilePath("");
			}
			//配偶
			if(rcCstSocialRlVO.getRlTypCd().equals(RL_TYP_CD_5) && !cfgCstIdCdMateMap.isEmpty() && cfgCstIdCdMateMap.get(cstId) != null){
				rcCstSocialRlVO.setIdCardFileNm(cfgCstIdCdMateMap.get(cstId).getFileNm());
				rcCstSocialRlVO.setIdCardFilePath(cfgCstIdCdMateMap.get(cstId).getFilePath());
			}else{
				rcCstSocialRlVO.setIdCardFileNm("");
				rcCstSocialRlVO.setIdCardFilePath("");
			}
			
		}
	}

}
